#include "SysLoginForm.h"
#include "ui_SysLoginForm.h"
#include "SysMainForm.h"
#include <QMessageBox>
#include "SysDAL.h"
#include <QPainter>
#include <QStatusBar>
#include <QDebug>

SysDBL *client=new SysDBL ();//实例化数据库访问对象
QSqlDatabase conn=client->SqlConnectClient(); //创建数据库连接
SysLoginForm::SysLoginForm(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SysLoginForm)
{
    ui->setupUi(this);
    this->setWindowState(Qt::WindowMaximized);
    this->setStyleSheet("#frame{border-radius: 10px;}");
    ui->frame->setStyleSheet("background: rgba(200,200,200, 0.75);");
    ui->lineUser->setStyleSheet("background: rgba(255,255,255, 0.8);");
    ui->linePasswd->setStyleSheet("background: rgba(255, 255, 255, 0.8);");
    ui->BtnLogin->setStyleSheet("background:green ;border-radius: 8px;color:white;");
    ui->BtnClose->setStyleSheet("background:red ;border-radius: 8px;color:white;");
    const QString FalseStyle = "min-width: 16px; min-height: 16px;max-width:16px; max-height: 16px;border-radius: 8px;  border:1px solid white;background:red";
    const QString TrueStyle = "min-width: 16px; min-height: 16px;max-width:16px; max-height: 16px;border-radius: 8px;  border:1px solid white;background:green";
    QStatusBar *sBur =statusBar();
    StatusColor = new QLabel ();
    StatusText = new QLabel ();
    StatusText->setStyleSheet("color:white;");
    if(conn.open())
    {
        StatusColor->setStyleSheet(TrueStyle);
        StatusText->setText( QObject::tr("服务器连接成功"));

    }
    else
    {
        StatusColor->setStyleSheet(FalseStyle);
        StatusText->setText( QObject::tr("服务器连接失败"));
        qDebug()<<conn.lastError().text();
    }
    sBur->setStyleSheet("background: rgba(0,0,0, 0.5);");
    sBur->setSizeGripEnabled(false);
    sBur->addWidget(StatusColor);
    sBur->addWidget(StatusText);

}

SysLoginForm::~SysLoginForm()
{
    delete ui;

}

void SysLoginForm::paintEvent(QPaintEvent *event)
{

    QPainter p(this);
    p.drawPixmap(0,0,this->width(),this->height(),QPixmap(":/Sys/SysPicture/login.jpg"));

}

void SysLoginForm::on_BtnLogin_clicked()
{
    QString user=ui->lineUser->text();
    QString pwd=ui->linePasswd->text();
    if(user.isEmpty() || pwd.isEmpty())
    {
        QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("用输入用户名或密码"),QMessageBox::Ok);
    }
    else
    {
        ExecStatus login= client->Login(user,pwd);
        if(login.Status){
            QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("登录成功^_^"),QMessageBox::Ok);
            MainForm *mainWin=new MainForm ();
            mainWin->show();
            this->hide();
        }
        else{
            ui->linePasswd->clear();
            QMessageBox::warning(this,QObject::tr("提示"),login.StatusInfo);
        }
    }
}

void SysLoginForm::on_BtnClose_clicked()
{
    this->close();
}
